package com.bt.lms.ui.page.sys.study;

import com.bt.lms.constants.PageConstant;
import com.bt.lms.entity.StudyCen;
import com.bt.lms.entity.page.Page;
import com.bt.lms.ui.page.common.PageLocation;
import com.bt.lms.ui.page.sys.utils.NetWorkUtils;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.main.BotLmsApp;
import com.bt.lms.ui.page.sys.utils.MyButton;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

/**
 * 学习中心详情界面
 *
 * @author 徐大永
 */
public class PageDetails extends VBox {

    /**
     * 界面跳转信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 要查看详情的学习中心列表对象
     */
    private final StudyCen studyCen;

    /**
     * 构建界面
     *
     * @param studyCen     学习中心
     * @param pageSkipInfo 界面跳转信息
     */
    public PageDetails(StudyCen studyCen, PageSkipInfo pageSkipInfo) {

        //维护
        this.studyCen = studyCen;
        this.pageSkipInfo = pageSkipInfo;

        //大小
        this.setPrefWidth(BotLmsApp.PER_WIGHT);
        this.setPrefHeight(BotLmsApp.HEIGHT);

        //组件
        //定位区域
        PageLocation pageLocation = new PageLocation("系统管理", "学习中心管理", "学习中心详情");
        this.getChildren().add(pageLocation);

        //输入区域
        MyVBox inputVBox = new MyVBox();
        inputVBoxInit(inputVBox);
        ObservableList<Node> inputChildren = this.getChildren();
        inputChildren.add(inputVBox);

        //控制区域
        MyHBox controlHBox = new MyHBox();
        controlHBoxInit(controlHBox);
        ObservableList<Node> controlChildren = this.getChildren();
        controlChildren.add(controlHBox);
    }

    /**
     * 输入区域初始化
     *
     * @param inputVBox 输入区域
     */
    private void inputVBoxInit(MyVBox inputVBox) {

        //布局
        Insets insets = new Insets(PageConstant.PANE_TINY_PADDING);
        inputVBox.setPadding(insets);
        inputVBox.setSpacing(PageConstant.PANE_TINY_SPACING);
        inputVBox.setAlignment(Pos.CENTER_LEFT);

        //组件（位置）
        Label locationLab = new Label("新增学习中心");
        //组件添加
        ObservableList<Node> locationChildren = inputVBox.getChildren();
        locationChildren.add(locationLab);

        //组件（输入网格
        GridPane gridPane = inputGrid();
        inputVBox.getChildren().add(gridPane);
    }

    /**
     * 网格布局
     *
     * @return 网格布局
     */
    private GridPane inputGrid() {

        //新建网格布局
        GridPane res = new GridPane();

        //数据控制
        int row = 0;
        int column;

        //大小
        res.setPrefWidth(BotLmsApp.PER_WIGHT);
        res.setPadding(new Insets(PageConstant.GRID_PANE_PADDING));
        res.setHgap(PageConstant.GRID_PANE_H);//间隙（左右）
        res.setVgap(PageConstant.GRID_PANE_H);//间隙（上下）

        //学习中心名称 coding
        row++;
        column = 0;
        Label nameLab = new Label("学习中心名称：");
        Label nameTF = new Label(studyCen.getName());
        res.add(nameLab, column++, row);
        res.add(nameTF, column, row);
        GridPane.setHalignment(nameLab, HPos.RIGHT);

        //学习中心编码 code
        row++;
        column = 0;
        Label codingLab = new Label("学习中心编码：");
        Label codingTF = new Label(studyCen.getCode());
        res.add(codingLab, column++, row);
        res.add(codingTF, column, row);
        GridPane.setHalignment(codingLab, HPos.RIGHT);

        //所在地
        row++;
        column = 0;
        String address = studyCen.getAddress();
        Label addressLab = new Label("所在地：");
        res.add(addressLab,column++,row);
        Label addressDataLab = new Label(address);
        res.add(addressDataLab,column,row);

        //联系人 relation
        row++;
        column = 0;
        Label relationPenLab = new Label("联系人：");
        Label relationPenTF = new Label(studyCen.getRelationPen());
        res.add(relationPenLab, column++, row);
        res.add(relationPenTF, column, row);
        GridPane.setHalignment(relationPenLab, HPos.RIGHT);

        //移动电话 phone
        row++;
        column = 0;
        Label phoneLab = new Label("移动电话：");
        Label phonePenTF = new Label(studyCen.getPhone());
        res.add(phoneLab, column++, row);
        res.add(phonePenTF, column, row);
        GridPane.setHalignment(phoneLab, HPos.RIGHT);

        //电子邮箱 email
        row++;
        column = 0;
        Label emailLab = new Label("电子邮箱：");
        Label emailTF = new Label(studyCen.getEmail());
        res.add(emailLab, column++, row);
        res.add(emailTF, column, row);
        GridPane.setHalignment(emailLab, HPos.RIGHT);

        //学习中心描述 describe
        row++;
        column = 0;
        Label describeLab = new Label("学习中心描述：");
        Label describeTA = new Label(studyCen.getDescribe());
        res.add(describeLab, column++, row);
        res.add(describeTA, column, row);
        GridPane.setHalignment(describeLab, HPos.RIGHT);

        return res;
    }

    /**
     * 控制区域初始化
     *
     * @param controlHBox 控制区域
     */
    private void controlHBoxInit(MyHBox controlHBox) {

        //布局
        Insets insets = new Insets(10);
        controlHBox.setPadding(insets);
        controlHBox.setSpacing(10);
        controlHBox.setAlignment(Pos.CENTER);

        //组件(取消)
        MyButton cancelBtn = new MyButton("返回");
        //大小控制
        Insets cancelBtnInsets = new Insets(10, 20, 10, 20);
        cancelBtn.setPadding(cancelBtnInsets);
        //组件添加
        ObservableList<Node> cancelBtnChildren = controlHBox.getChildren();
        cancelBtnChildren.add(cancelBtn);
        //事件
        cancelBtn.setOnAction(event -> NetWorkUtils.reqStudyCenPagingPage(pageSkipInfo,new Page<>()));

    }
}
